/**
 * 数据分页
 */
class Paging {
	// 页码：默认第一页
	page = 1
	// 请求参数
	req
	// 请求开关
	locker = false
	// 是否还有数据
	moreData = true
	// 总数居
	accumulator = []
	constructor(req) {
		this.req = req
	}
	async getMoreData() {
		if(!this.moreData){
			return
		}
		if(!this._getLocker()){
			return
		}
		const data = await this._actualGetData()
		this._releaseLocker()
		return data
	}
	// 请求开关
	_getLocker() {
		if (this.locker) {
			return false
		}
		this.locker = true
		return true
	}
	// 打开请求
	_releaseLocker() {
		this.locker = false
	}
	async _actualGetData() {
		const req = this._getCurrentReq()
		let paging = await Paging.http.request(this.req)
		if(!paging){
			return null
		}
		if(paging.total === 0){
			return {
				empty:true,
				data:[],
				moreData:false,
				accumulator:[],
				total:paging.total,
				info:paging.info || null
			}
		}
		this.moreData = Paging._moreData(paging.last_page, this.page)
		if(this.moreData){
			this.page += 1
		}
		this._accumulate(paging.data)
		return {
			empty:false,
			data: paging.data,
			moreData:this.moreData,
			accumulator:this.accumulator,
			total:paging.total,
			info:paging.info || null
		}
	}
	// 追加数据
	_accumulate(data){
	        this.accumulator = this.accumulator.concat(data)
	}
	// 判断页码和总页码
	static _moreData(totalPage, pageNum) {
		return pageNum < totalPage
	}
	// 处理请求参数
	_getCurrentReq() {
		if(this.req.data){
			this.req.data.page = this.page
		}else{
			this.req.data = {page:this.page}
		}
		return this.req
	}
	
}
export default Paging